<template>
  <div class="notification-container">
    <Button 
      icon="pi pi-bell" 
      class="notification-button"
      @click="toggleNotificationPanel"
      :aria-label="'通知'"
    >
      <Badge 
        v-if="unreadCount > 0" 
        :value="unreadCount > 99 ? '99+' : unreadCount"
        severity="danger"
        class="notification-badge"
      />
    </Button>
    
    <!-- 通知面板 -->
    <div v-if="showNotificationPanel" class="notification-panel">
      <div class="notification-header">
        <h3>消息通知</h3>
        <Button 
          icon="pi pi-times" 
          class="close-button"
          @click="closeNotificationPanel"
        />
      </div>
      
      <div class="notification-content">
        <div v-if="notifications.length === 0" class="empty-notifications">
          <i class="pi pi-bell-slash"></i>
          <p>暂无消息</p>
        </div>
        
        <div v-else class="notification-list">
          <div 
            v-for="notification in notifications" 
            :key="notification.id"
            class="notification-item"
            :class="{ 'unread': !notification.read }"
            @click="markAsRead(notification.id)"
          >
            <div class="notification-icon">
              <i :class="getNotificationIcon(notification.type)"></i>
            </div>
            <div class="notification-content">
              <div class="notification-title">{{ notification.title }}</div>
              <div class="notification-message">{{ notification.message }}</div>
              <div class="notification-time">{{ formatTime(notification.createdAt) }}</div>
            </div>
            <div v-if="!notification.read" class="unread-dot"></div>
          </div>
        </div>
      </div>
      
      <div class="notification-footer">
        <Button 
          label="全部标记为已读" 
          class="mark-all-read-button"
          @click="markAllAsRead"
          :disabled="unreadCount === 0"
        />
        <Button 
          label="查看全部" 
          class="view-all-button"
          @click="viewAllNotifications"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useNotificationStore } from '../../store/modules/notification'
import Button from 'primevue/button'
import Badge from 'primevue/badge'

// 通知store
const notificationStore = useNotificationStore()

// 通知面板显示状态
const showNotificationPanel = ref(false)

// 计算属性
const notifications = computed(() => notificationStore.notifications)
const unreadCount = computed(() => notificationStore.unreadCount)

// 切换通知面板
const toggleNotificationPanel = () => {
  showNotificationPanel.value = !showNotificationPanel.value
  if (showNotificationPanel.value) {
    // 打开面板时刷新通知列表
    notificationStore.fetchNotifications()
  }
}

// 关闭通知面板
const closeNotificationPanel = () => {
  showNotificationPanel.value = false
}

// 标记为已读
const markAsRead = (id: string) => {
  notificationStore.markAsRead(id)
}

// 全部标记为已读
const markAllAsRead = () => {
  notificationStore.markAllAsRead()
}

// 查看全部通知
const viewAllNotifications = () => {
  closeNotificationPanel()
  // 这里可以跳转到通知列表页面
}

// 获取通知图标
const getNotificationIcon = (type: string) => {
  const iconMap: Record<string, string> = {
    'info': 'pi pi-info-circle',
    'success': 'pi pi-check-circle',
    'warning': 'pi pi-exclamation-triangle',
    'error': 'pi pi-times-circle',
    'system': 'pi pi-cog'
  }
  return iconMap[type] || 'pi pi-bell'
}

// 格式化时间
const formatTime = (date: string) => {
  const now = new Date()
  const notificationDate = new Date(date)
  const diff = now.getTime() - notificationDate.getTime()
  
  const minutes = Math.floor(diff / (1000 * 60))
  const hours = Math.floor(diff / (1000 * 60 * 60))
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  if (minutes < 1) return '刚刚'
  if (minutes < 60) return `${minutes}分钟前`
  if (hours < 24) return `${hours}小时前`
  if (days < 7) return `${days}天前`
  
  return notificationDate.toLocaleDateString()
}

// 点击外部关闭面板
onMounted(() => {
  document.addEventListener('click', (e) => {
    const container = document.querySelector('.notification-container')
    if (container && !container.contains(e.target as Node)) {
      showNotificationPanel.value = false
    }
  })
  
  // 初始化通知数据
  notificationStore.fetchNotifications()
})
</script>

<style scoped>
.notification-container {
  position: relative;
  margin-right: 10px;
}

.notification-button {
  position: relative;
  background: transparent;
  border: none;
  color: #333;
  padding: 8px;
}

.notification-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 10px;
  min-width: 16px;
  height: 16px;
  line-height: 16px;
}

.notification-panel {
  position: absolute;
  top: 100%;
  right: 0;
  width: 360px;
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  margin-top: 8px;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #e0e0e0;
}

.notification-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.close-button {
  background: transparent;
  border: none;
  color: #666;
  padding: 4px;
}

.notification-content {
  max-height: 400px;
  overflow-y: auto;
}

.empty-notifications {
  text-align: center;
  padding: 40px 20px;
  color: #999;
}

.empty-notifications i {
  font-size: 48px;
  margin-bottom: 16px;
  display: block;
}

.notification-list {
  padding: 8px 0;
}

.notification-item {
  display: flex;
  align-items: flex-start;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.2s;
  position: relative;
}

.notification-item:hover {
  background-color: #f5f5f5;
}

.notification-item.unread {
  background-color: #f0f8ff;
}

.notification-icon {
  margin-right: 12px;
  margin-top: 2px;
}

.notification-icon i {
  font-size: 16px;
  color: #666;
}

.notification-content {
  flex: 1;
  min-width: 0;
}

.notification-title {
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
  font-size: 14px;
}

.notification-message {
  color: #666;
  font-size: 13px;
  line-height: 1.4;
  margin-bottom: 4px;
  word-break: break-word;
}

.notification-time {
  color: #999;
  font-size: 12px;
}

.unread-dot {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 8px;
  height: 8px;
  background-color: #ff4d4f;
  border-radius: 50%;
}

.notification-footer {
  padding: 12px 16px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  gap: 8px;
}

.mark-all-read-button,
.view-all-button {
  flex: 1;
  font-size: 12px;
  padding: 6px 12px;
}

.mark-all-read-button {
  background: #f0f0f0;
  border: 1px solid #d9d9d9;
  color: #666;
}

.view-all-button {
  background: #1890ff;
  border: 1px solid #1890ff;
  color: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .notification-panel {
    width: 320px;
    right: -10px;
  }
}
</style>
